package com.example.person;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;

@Service
//@DefaultProperties(defaultFallback="getPoliceFallback") 
//默认的回退方法是没有参数的
public class PoliceService {

	@Autowired
	private RestTemplate restTemplate;
	
	/**
	 * group key 用于线程
	 * commandKey
	 * commandProperties 超时时间 默认值是1s
	 * threadPoolProperties 线程池
	 * @param id
	 * @return
	 */
	@HystrixCommand(fallbackMethod="getPoliceFallback",commandProperties={
			@HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds",value="2000")
	},threadPoolProperties={
			@HystrixProperty(name="coreSize",value="2")
	})
	public Police getPolice(Integer id){
		return restTemplate.getForObject("http://spring-lb-provider/call/1",Police.class);
	}
	
	/**
	 * 回退方法
	 */
	public Police getPoliceFallback(Integer id){
		Police police=new Police();
		police.setId(id);
		police.setName("111");
		return police;
	}
	
}
